package com.qzsoft.zhjd.server.modules.ludong.asphalt.controller;


import com.qzsoft.zhjd.server.modules.ludong.asphalt.service.AsphaltRelationService;
import com.qzsoft.zhjd.server.common.utils.MapToBeanUtils;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.ludong.asphalt.entity.AsphaltRelationEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 *
 * 功能描述:沥青理论配合比与生产采集表关联
 * @author jiangyafei
 * @email jiangyf_sd@163.com
 * @date 2020-06-09 11:17:43
 */
@Slf4j
@RestController
@RequestMapping("datacollect/asphalt/asphaltrelation")
public class AsphaltRelationController {
    @Autowired
    private AsphaltRelationService asphaltRelationService;


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestParam Map<String,Object> params){
        log.info("沥青理论配合比与生产采集表关联==="+params.toString());
        if(params !=null && params.size()>0 ){
            Map<String,Object> map = MapToBeanUtils.toReplaceKeyLow(params);
            try {
                AsphaltRelationEntity asphaltRelation = MapToBeanUtils.mapToObject(map,AsphaltRelationEntity.class);
                AsphaltRelationEntity asphaltRelationEntity = asphaltRelationService.getById(asphaltRelation.getId());
                if(asphaltRelationEntity != null && asphaltRelationEntity.getId() !=null && !"".equals(asphaltRelationEntity.getId())){
                    asphaltRelationService.removeById(asphaltRelationEntity.getId());
                }
                asphaltRelationService.save(asphaltRelation);
                return R.ok();
            }catch (Exception e){
                return R.error(400,e.getMessage());
            }
        }
        return R.error(400,"数据为空！");
    }


}
